package com.haotunet.app.goldtreasure.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.adapter.MediaReportAdapter;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.MediaInfo;
import com.haotunet.app.goldtreasure.model.MediaModel;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.pulltorefresh.PullToRefreshLayout;
import com.haotunet.app.pulltorefresh.pullableView.PullableListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class MediaReportFragment extends BaseFragment {

    @InjectView(R.id.lv_media_report)
    PullableListView lvMediaReport;
    @InjectView(R.id.id_swipe_ly)
    PullToRefreshLayout refreshLayout;
    private MediaReportAdapter mediaReportAdapter;
    private int pageNum = 20;
    private int type;

    public MediaReportFragment() {

    }

    public static MediaReportFragment newInstance(int type) {
        MediaReportFragment fragment = new MediaReportFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_media_report, container, false);
        ButterKnife.inject(this, view);
        initView();
        return view;
    }

    private void initView() {
        getMediaReport(type, "0", "");
        RefreshData(0);
    }

    private void RefreshData(final int pagenum) {
        refreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
                getMediaReport(type, pagenum + "", "refresh");
                pageNum = 20;
            }

            @Override
            public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
                getMediaReport(type, pageNum + "", "loadmore");
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    public void getMediaReport(int type, String pagenum, final String action) {
        ActionUtil actionUtil = new ActionUtil(getActivity());
        actionUtil.getDiscoverNews(type, pagenum);
        actionUtil.setOnPassBackListener(new Interface.OnPassBackListener() {
            @Override
            public void passBack(BaseModel model) {
                if (!isActivityAvailable()) {
                    return;
                }
                MediaModel mediaModel = (MediaModel) model;
                if (mediaModel == null) {
                    UIHelper.showToast(getActivity(), getString(R.string.rank_fail));
                    return;
                }
                if (mediaModel.getErrorMsg() != null) {
                    UIHelper.showToast(getActivity(), mediaModel.getErrorMsg());
                } else {

                    if (action.equals("refresh") || action.equals("loadmore")) {
                        refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    }
                    if (action.equals("loadmore")) {
                        pageNum = pageNum + 20;
                        setAdapterData(mediaModel);
                    } else {
                        loadMoreData(mediaModel);
                    }

                }
            }
        });
    }

    private void loadMoreData(MediaModel mediaModel) {
        List<MediaInfo> articleList = mediaModel.getArticleList();
        mediaReportAdapter = new MediaReportAdapter(getActivity(), articleList);
        lvMediaReport.setAdapter(mediaReportAdapter);
    }

    private void setAdapterData(MediaModel mediaModel) {
        if (mediaModel.getArticleList().size() == 0) {
            UIHelper.showToast(getActivity(), getString(R.string.last_page_hint));
        }
        mediaReportAdapter.addDatas(mediaModel.getArticleList());
    }

    @Override
    protected void lazyLoad() {

    }
}
